<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
	<div class="mar-st-a30">
		<form class="form-horizontal" id="widgetSnaphsotSbumit"
			action="{{ctx}}/mam/widgetVersion/cloneAdd"
			enctype="multipart/form-data">
			<input id="cloneVersionId" type="hidden" value="{{versionVO.id}}" />
			<input id="release_not" type="hidden" value="" name="release_not" />
			<input id="build" type="hidden" value="" name="build" /> <input
				id="dependsOn" type="hidden" value="" name="dependsOn" /> <input
				id="version" type="hidden" value="" name="version" /> <input
				id="size" type="hidden" value="0" name="size" /> <input
				id="identifier" type="hidden"
				value="{{versionVO.widgetVo.identify}}" name="identifier" />
			<table align="center" width="100%">
				<tr>
					<th class="text-right" width="20%">克隆对象:</th>
					<td class="text-left"><span class="label label-info">{{versionVO.widgetVo.name}}</span>
						<span class="label label-info" id="cloneVersionText">{{versionVO.version}}</span><br /></td>
				</tr>
				<tr style="line-height: 50px">
					<th class="text-right" width="20%">安装包:</th>
					<td class="text-left"><div id="packUploadContainer"></div></td>
				</tr>

			</table>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	var popover = function(inputId, content) {
		var options = {
			placement : "bottom",
			content : content
		};
		$('#' + inputId).attr("data-toggle", "popover");
		$('#' + inputId).popover(options);
		$('#' + inputId).popover('show');
	}

	$(function() {
		var uploader = buildPlupload({
			id : "packUploadContainer",
			elmName : "boundle",
            filters : [{title : "模块包", extensions : "zip"}],
			targetUrl : "{{ctx}}/mam/attachment/upload",
			defaultVal : "",
			btnName : "上传模块包"
		});

		uploader.bind('FileUploaded', function(up, files, info) {
			var obj = eval("(" + info.response + ")");
			var id = obj.id;
			$('#packUploadContainerInput').val(id);
			var size = $('#packUploadContainerSize').val();
			$('#size').val(size);

			var tel = /.zip$/;
			var value = jQuery("#packUploadContainerName").val();
			if (value != null && value != "") {
				if (!tel.test(value)) {
					popover("packUploadContainerBtn", "请上传zip格式文件！");
					$('#packUploadContainerInput').val("");
					return false;
				}
			}
			readFileAction(id);

		});
	})

	function readFileAction(id) {
		var cloneidentifier = $("#identifier").val();
		var cloneversion = "{{versionVO.version}}";
		$.ajax({
			async : true,
			type : "GET",
			url : "{{ctx}}/mam/attachment/readfile/" + id,
			dataType : 'json',
			success : function(data, textStatus) {
				var identifier = data.identifier;
				var version = data.version
				if (cloneidentifier != identifier) {
					$('#packUploadContainerInput').val("");
					popover("packUploadContainerBtn", "模块标识不正确,请重新上传模块包！");
				}
				identifierVersion(identifier, version);

				jQuery("#version").val(data.version);
				jQuery("#dependsOn").val(data.dependsOn);
				jQuery("#build").val(data.build);
				jQuery("#release_not").val(data.releaseNote);

			},
			error : function(data, status, e) {
				bootbox.alert("读取模块配置失败，请稍后再试！");
			}
		});
	}
	//检查版本号是否存在
	function identifierVersion(identifier, version) {
		$.ajax({
			async : true,
			type : "GET",
			url : "{{ctx}}/mam/widgetVersion/" + identifier + "/" + version+"/",
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "true") {
					$('#packUploadContainerInput').val("");
					popover("packUploadContainerBtn", "模块版本已经存在，请重新上传模块包！");
					return;
				}
			},
			error : function(data, status, e) {
				bootbox.alert("检查版本号失败，请稍后再试！");
			}
		});
	}

	function widgetCloneSbumit() {
		var value = $('#packUploadContainerInput').val();
		if (value == "") {
			popover("packUploadContainerBtn", "请上传模块zip包！");
			return false;
		}
		var data = $('#widgetSnaphsotSbumit').serialize();
		var cloneVersionId = $('#cloneVersionId').val();
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/widgetVersion/cloneAdd/" + cloneVersionId,
			data : data,
			dataType : 'json',
			success : function(result, textStatus) {
				if (result.result == "success") {
					window.location.reload();
				} else {
					bootbox.alert("添加失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
	}
</script>
